package com.tcp.c100w.c100w;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.tcp.c100w.c100w.processor.DefaultRequestProcessor;
import com.tcp.c100w.c100w_remoting.ChannelEventListener;
import com.tcp.c100w.c100w_remoting.RemotingServer;
import com.tcp.c100w.c100w_remoting.common.ThreadFactoryImpl;
import com.tcp.c100w.c100w_remoting.netty.NettyRemotingAbstract;
import com.tcp.c100w.c100w_remoting.netty.NettyRemotingServer;
import com.tcp.c100w.c100w_remoting.netty.NettyServerConfig;
import com.tcp.c100w.c100w_remoting.utils.SystemTimer;
import io.netty.channel.Channel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class C100kController {
    private NettyServerConfig config = null;
    private RemotingServer remotingServer;
    private ExecutorService remotingExecutor;
    private final ScheduledExecutorService scheduledExecutorService = Executors
            .newSingleThreadScheduledExecutor(new ThreadFactoryImpl("NSScheduledThread"));
    public static int sleep = 0;
    protected Logger logger=LogManager.getLogger(C100kController.class.getName());
    public C100kController(NettyServerConfig config) {
        this.config = config;
    }

    public void shutdown() {
        this.remotingServer.shutdown();
        this.remotingExecutor.shutdown();
        this.scheduledExecutorService.shutdown();
    }

    public void start() {
        this.remotingServer.start();
    }

    public boolean initialize() {
        this.remotingServer = new NettyRemotingServer(this.config, new ChannelEventListener() {

            @Override
            public void onChannelConnect(String s, Channel channel) {
                //System.out.println("onChannelConnect");
            }

            @Override
            public void onChannelClose(String s, Channel channel) {
                //System.out.println("onChannelClose");
            }

            @Override
            public void onChannelException(String s, Channel channel) {
                //System.out.println("onChannelException");
            }

            @Override
            public void onChannelIdle(String s, Channel channel) {
                //System.out.println("onChannelIdle");
            }
        });
        this.remotingExecutor = Executors.newFixedThreadPool(config.getProcessosThread(),
                new ThreadFactoryImpl("RemotingExecutorThread_"));
        this.registerProcessor();
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {

            public void run() {
                int sleepMath = ((int) (Math.random() * 10));
                sleep = sleepMath > 5 ? (sleepMath - 5) : sleepMath;
                logger.info("-------sleep:" + sleep + " channelsize:" + remotingServer.getChannleSize());

            }
        }, 5, 10, TimeUnit.SECONDS);
        return true;
    }

    private void registerProcessor() {
        this.remotingServer.registerDefaultProcessor(new DefaultRequestProcessor(this), remotingExecutor);
    }

}
